package de.wittekind.bundesjugendspiele.client.management.ui.view;

import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.HashMap;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTree;
import javax.swing.TransferHandler;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;

import de.wittekind.bundesjugendspiele.client.management.ui.UIController;
import de.wittekind.bundesjugendspiele.server.interfaces.Riege;
import de.wittekind.bundesjugendspiele.server.interfaces.Schueler;

public class RiegenView extends JTree {

	private HashMap<Riege, BujuNode> r_b_assoc;
	private HashMap<Schueler, BujuNode> s_b_assoc;
	JMenuItem b;

	private static final long serialVersionUID = -6165148189709299257L;

	JPopupMenu rightClickMen;

	RiegenView(UIController ui) {
		super(new DefaultMutableTreeNode());
		r_b_assoc = new HashMap<Riege, BujuNode>();
		s_b_assoc = new HashMap<Schueler, BujuNode>();
		getSelectionModel().setSelectionMode(
				TreeSelectionModel.SINGLE_TREE_SELECTION);

		setTransferHandler(new RiegenTreeTransferHandler());

		rightClickMen = new JPopupMenu();

		b = new JMenuItem("Delete");
		b.setName("delete");
		

		rightClickMen.add(b);

		addMouseListener(new RiegenViewMouseListener());
	}

	public void refresh() throws IOException {

		
		DefaultMutableTreeNode root = (DefaultMutableTreeNode) treeModel
				.getRoot();

		updateNodeRec(root);

	}

	private void updateNodeRec(TreeNode n) throws IOException {
		if (n.getParent() != null) {
			((BujuNode) n).update(this);
		}
		if (n.getChildCount() != 0) {
			for (int i = 0; i < n.getChildCount(); i++) {
				updateNodeRec(((BujuNode) n.getChildAt(i)));
			}
		}
	}

	public void addBujuNode(BujuNode b, boolean selected) {
		if (b.getSchueler() == null) {
			((DefaultMutableTreeNode) this.treeModel.getRoot()).add(b);
			r_b_assoc.put(b.getRiege(), b);
		} else {
			BujuNode rN = r_b_assoc.get(b.getRiege());
			rN.add(b);
			s_b_assoc.put(b.getSchueler(), b);
		}
	}
	
	public void remove(Riege r, Schueler s) {
		r_b_assoc.get(r).remove(s_b_assoc.get(s));
		updateUI();
	}
	
	public void remove(Riege r) {
		//((DefaultMutableTreeNode) treeModel.getRoot()).remove(r_b_assoc.get(r));
		updateUI();
	}

	private class RiegenTreeTransferHandler extends TransferHandler {

		/**
		 * 
		 */
		private static final long serialVersionUID = -3248243726249024230L;

		public RiegenTreeTransferHandler() {
			super();
		}

	}

	protected class RiegenViewMouseListener extends MouseAdapter {
		public void mousePressed(MouseEvent e) {
			if (e.isPopupTrigger()) {
				
				rightClickMen.show(e.getComponent(), e.getX(), e.getY());
			}
		}

		public void mouseReleased(MouseEvent e) {
			if (e.isPopupTrigger()) {
				rightClickMen.show(e.getComponent(), e.getX(), e.getY());
			}
		}
	}
	
	public void addActionListener(ActionListener a) {
		b.addActionListener(a);
	}

}